package cn.yuanqiao.web.controller.archive.appManager;

import cn.yuanqiao.archive.appManager.domain.TenantFields;
import cn.yuanqiao.archive.appManager.service.ITenantFieldsService;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/archive/yqfields")
public class TenantFieldsController extends BaseController {

    @Autowired
    private ITenantFieldsService tenantFieldsService;
    /**
     * 查询基础数据列表
     */
    //@RequiresPermissionsrmissions("archive:yqfields:list")
    @Log(title = "查询基础数据列表",businessType = BusinessType.SELECT)
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody TenantFields yqfields)
    {
        startPage();
        List<TenantFields> list = tenantFieldsService.selectyqfieldsList(yqfields);
        return getDataTable(list);
    }

    @Log(title = "查询基础数据列表",businessType = BusinessType.SELECT)
    @GetMapping("/listNoPage")
    public TableDataInfo listNoPage(TenantFields yqfields)
    {
        //startPage();
        List<TenantFields> list = tenantFieldsService.selectyqfieldsList(yqfields);
        return getDataTable(list);
    }

    /**
     * 导出基础数据列表
     */
    //@RequiresPermissionsrmissions("archive:yqfields:export")
    @Log(title = "导出基础数据列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TenantFields yqfields)
    {
        List<TenantFields> list = tenantFieldsService.selectyqfieldsList(yqfields);
        ExcelUtil<TenantFields> util = new ExcelUtil<TenantFields>(TenantFields.class);
        util.exportExcel(response, list, "基础数据");
    }

    /**
     * 获取基础数据详细信息
     */
    //@RequiresPermissionsrmissions("archive:yqfields:query")
    @Log(title = "获取基础数据详细信息",businessType = BusinessType.SELECT)
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(tenantFieldsService.selectyqfieldsByID(ID));
    }

    /**
     * 新增基础数据
     */
    //@RequiresPermissionsrmissions("archive:yqfields:add")
    @Log(title = "新增基础数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TenantFields yqfields)
    {
        return toAjax(tenantFieldsService.insertyqfields(yqfields));
    }

    @Log(title = "导入模板下载", businessType = BusinessType.DOWNLOAD)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<TenantFields> util = new ExcelUtil<TenantFields>(TenantFields.class);
        util.importTemplateExcel(response, "基础数据");
    }
    /**
     * 修改基础数据
     */
    //@RequiresPermissionsrmissions("archive:yqfields:edit")
    @Log(title = "修改基础数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TenantFields yqfields)
    {
        return toAjax(tenantFieldsService.updateyqfields(yqfields));
    }

    /**
     * 单独修改展示类型
     * @param yqfields
     * @return
     */
    @Log(title = "修改基础数据列表下拉", businessType = BusinessType.UPDATE)
    @PutMapping("/editFieldTypeSelect")
    public AjaxResult editFieldTypeSelect(@RequestBody TenantFields yqfields)
    {
        return toAjax(tenantFieldsService.updateyqfields(yqfields));
    }

    /**
     * 删除基础数据
     */
    //@RequiresPermissionsrmissions("archive:yqfields:remove")
    @Log(title = "删除基础数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(tenantFieldsService.deleteyqfieldsByIDs(IDs));
    }

    //@RequiresPermissionsrmissions("manage:tenant:import")
    @Log(title = "基础数据导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String type) throws Exception
    {
        ExcelUtil<TenantFields> util = new ExcelUtil<>(TenantFields.class);
        List<TenantFields> userList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = tenantFieldsService.importUser(userList, updateSupport, operName,type);
        return success(message);
    }


}
